/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Viewer.java
 *
 * Created on Nov 16, 2011, 11:51:32 PM
 */
/**
 *
 * @author Sujal
 */
import java.lang.reflect.Array;
import java.util.Vector;
import javax.swing.table.*;
import javax.swing.*;
import java.util.Random;
import java.util.Collections;
import java.util.HashSet;

public class Viewer extends javax.swing.JDialog {

    private boolean addAthletesClicked;
    private boolean saveData;
    private nanduData nandu;
    private Vector AthleteListFileDataOrdered;
    private Vector AthleteOriginalIndexOrdered;
    //private Vector AthleteListFileDataRaw;
    
    /** Creates new form Viewer */
    public Viewer(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        init();
        dataT.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE); 
        AthleteListFileDataOrdered=new Vector();
        AthleteOriginalIndexOrdered=new Vector();
        //AthleteListFileDataRaw=new Vector();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        dataT = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        SaveB = new javax.swing.JButton();
        CancelB = new javax.swing.JButton();
        Event = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        RandOrderB = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        Group = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Athlete Viewer");
        setAlwaysOnTop(true);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setIconImage(null);
        setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Database"));

        jScrollPane1.setAutoscrolls(true);

        dataT.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4", "Title 5"
            }
        ));
        dataT.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jScrollPane1.setViewportView(dataT);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 763, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 336, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        SaveB.setText("Save");
        SaveB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveBActionPerformed(evt);
            }
        });

        CancelB.setText("Cancel");
        CancelB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelBActionPerformed(evt);
            }
        });

        Event.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EventActionPerformed(evt);
            }
        });

        jLabel1.setText("Event");

        RandOrderB.setText("Randomize Order");
        RandOrderB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RandOrderBActionPerformed(evt);
            }
        });

        jLabel2.setText("Age/Exp/Gender");

        Group.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GroupActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Group, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(Event, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addComponent(RandOrderB)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SaveB)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(CancelB)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CancelB)
                    .addComponent(SaveB)
                    .addComponent(Event, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(RandOrderB)
                    .addComponent(jLabel2)
                    .addComponent(Group, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel3, java.awt.BorderLayout.PAGE_START);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 771, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 35, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void RandOrderBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RandOrderBActionPerformed

       String groupGender = (String) Group.getSelectedItem();
       String currEvent=((String) Event.getSelectedItem());
       if(groupGender==null || groupGender.length()==0 || currEvent == null || currEvent.equals(""))
            return;
       globalData.currentEvent.setevent(currEvent);
       globalData.currentAge=groupGender.substring(0, groupGender.indexOf('/'));//.toLowerCase();
       groupGender=groupGender.substring(groupGender.indexOf('/')+1, groupGender.length());
       globalData.currentGroup=groupGender.substring(0, groupGender.indexOf('/'));//.toLowerCase();
       globalData.currentGender=groupGender.substring(groupGender.indexOf('/')+1, groupGender.length());//.toLowerCase();
       
       String currentEvent=globalData.currentEvent.getevent();
       String currentAge=globalData.currentAge;
       String currentGroup=globalData.currentGroup;
       String currentGender=globalData.currentGender;
       
       
        Vector indices=new Vector();
        
        for(int i=0;i<AthleteListFileDataOrdered.size();i++)
        {
            Main.AthleteFileData ad=(Main.AthleteFileData)AthleteListFileDataOrdered.elementAt(i);
            if(ad.AgeGroup.equals(currentAge) && ad.Gender.equals(currentGender) && ad.Experience.equals(currentGroup) && ad.Event.equals(currentEvent))
            {
                indices.add(i);
            }            
        }
         
        //randomize values
        Random rand = new Random();
        for(int i=0;i<indices.size()*5;i++)
        {
            int rand0=rand.nextInt(indices.size());
            int rand1=rand.nextInt(indices.size());
            if(rand0==rand1)
                continue;
            int pos0=(int)(indices.get(rand0));
            int pos1=(int)(indices.get(rand1));
            //switches content of two rows
            Collections.swap(AthleteListFileDataOrdered,pos0,pos1);
            Collections.swap(AthleteOriginalIndexOrdered,pos0,pos1);
        }
        
        //update the list
        updateAthleteList();
        

//        Vector indices=new Vector();
//        //find position of all the current events
//        for(int i=0;i<dataT.getRowCount();i++)
//        {
//            String athleteGender=(String)dataT.getModel().getValueAt(i , 4);
//            String athleteAge=(String)dataT.getModel().getValueAt(i , 8);
//            String athleteGroup=(String)dataT.getModel().getValueAt(i , 7);
//            String athleteEvent=(String)dataT.getModel().getValueAt(i , 9);
//            
//            if(
//                    athleteGender == null || athleteGender.equals("") ||
//                    athleteAge == null || athleteAge.equals("") ||
//                    athleteGroup == null || athleteGroup.equals("") ||
//                    athleteEvent == null || athleteEvent.equals("")
//               )
//                continue;
//            if(
//                    athleteGender.toLowerCase().equals(currentGender.toLowerCase())==true &&
//                    athleteAge.toLowerCase().equals(currentAge.toLowerCase())==true &&
//                    athleteGroup.toLowerCase().equals(currentGroup.toLowerCase())==true &&
//                    athleteEvent.toLowerCase().equals(currEvent.toLowerCase())==true
//              )
//            {
//                indices.add(i);
//            }
//        }
//        //randomize values
//        Random rand = new Random();
//        for(int i=0;i<indices.size()*5;i++)
//        {
//            int rand0=rand.nextInt(indices.size());
//            int rand1=rand.nextInt(indices.size());
//            if(rand0==rand1)
//            continue;
//            int pos0=(int)(indices.get(rand0));
//            int pos1=(int)(indices.get(rand1));
//
//            //switches content of two rows
//            switchRows(pos0,pos1);
//        }
    }//GEN-LAST:event_RandOrderBActionPerformed

    private void CancelBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelBActionPerformed
        saveData=false;
        setVisible(false);
    }//GEN-LAST:event_CancelBActionPerformed

    private void SaveBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveBActionPerformed

        if(addAthletesClicked==true)
        {
            AthleteListFileDataOrdered.clear();
            //clear everything
            for(int i=0;i<dataT.getRowCount();i++)
            //for(int i=dataT.getRowCount()-2;i<dataT.getRowCount();i++)
            {
                Main.AthleteFileData ad=new Main.AthleteFileData();
                ad.AthleteID=(String)dataT.getModel().getValueAt(i , 0);
                ad.FirstName=(String)dataT.getModel().getValueAt(i , 1);
                ad.LastName=(String)dataT.getModel().getValueAt(i , 2);
                ad.Email=(String)dataT.getModel().getValueAt(i , 3);
                ad.Gender=(String)dataT.getModel().getValueAt(i , 4);
                ad.College=(String)dataT.getModel().getValueAt(i , 5);
                ad.School=(String)dataT.getModel().getValueAt(i , 6);
                ad.Experience=(String)dataT.getModel().getValueAt(i , 7);
                ad.AgeGroup=(String)dataT.getModel().getValueAt(i , 8);
                ad.Event=(String)dataT.getModel().getValueAt(i , 9);
                ad.Nandu=(String)dataT.getModel().getValueAt(i , 10);
                ad.SetupInfo=(String)dataT.getModel().getValueAt(i , 11);
                ad.JudgeTypes=(String)dataT.getModel().getValueAt(i , 12);
                ad.JudgeConnected=(String)dataT.getModel().getValueAt(i , 13);
                ad.JudgeARaw=(String)dataT.getModel().getValueAt(i , 14);
                ad.JudgeAAgree=(String)dataT.getModel().getValueAt(i , 15);
                ad.JudgeBRaw=(String)dataT.getModel().getValueAt(i , 16);
                ad.JudgeCRaw=(String)dataT.getModel().getValueAt(i , 17);
                ad.JudgeCAgree=(String)dataT.getModel().getValueAt(i , 18);
                ad.Time=(String)dataT.getModel().getValueAt(i , 19);
                ad.Deductions=(String)dataT.getModel().getValueAt(i , 20);
                ad.Reason=(String)dataT.getModel().getValueAt(i , 21);
                ad.Score=(String)dataT.getModel().getValueAt(i , 22);

                if(ad.AthleteID==null || ad.AthleteID.length()==0)
                continue;

                //fix and verify nandu
                ad.Nandu=ad.Nandu.trim();
                while(true)
                {
                    int spaceIndex=ad.Nandu.indexOf(' ');
                    if(spaceIndex==-1)
                    break;
                    ad.Nandu=ad.Nandu.substring(0,spaceIndex)+ad.Nandu.substring(spaceIndex+1,ad.Nandu.length());
                }
                ad.Nandu=ad.Nandu.replace(",,", ",");
                ad.Nandu=ad.Nandu.toUpperCase();
                //verify nandu
                if(verifyNandu(ad.Nandu,ad.Event)==false)
                {
                    dataT.getSelectionModel().setLeadSelectionIndex(i);
                    return;
                }

                //add the athlere list
                AthleteListFileDataOrdered.add(ad);
            }
        }

        saveData=true;
        setVisible(false);
    }//GEN-LAST:event_SaveBActionPerformed

    private void EventActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EventActionPerformed
        String currEvent=((String) Event.getSelectedItem());
        if(currEvent == null || currEvent.equals(""))
            return;
        globalData.currentEvent.setevent(currEvent);
        int startIndex=-1,endIndex=0;
        //find position of all the current events
        for(int i=0;i<dataT.getRowCount();i++)
        {
            String athleteEvent=(String)dataT.getModel().getValueAt(i , 9);
            if(athleteEvent == null || athleteEvent.equals(""))
                continue;
            if(athleteEvent.toLowerCase().equals(currEvent.toLowerCase())==true)
            {
                if(startIndex<0)
                    startIndex=i;
                endIndex=i;
            }
        }
        
        if(startIndex>=0)
        {
            ListSelectionModel tableSelectionModel = dataT.getSelectionModel();
            // set a selection interval (in this case the first row)
            tableSelectionModel.setSelectionInterval(startIndex,endIndex);
            //tableSelectionModel.addSelectionInterval(6,6);
            // update the selection model
            dataT.setSelectionModel(tableSelectionModel);
            // repaint the table
            dataT.repaint();
        }
        
        if(addAthletesClicked==false)
            updateAthleteList();
    }//GEN-LAST:event_EventActionPerformed

    private void GroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GroupActionPerformed
    
        String groupGender = (String) Group.getSelectedItem();
        //code changed it handles all cases
        if(groupGender==null ||groupGender.length()==0)
            return;
        
       globalData.currentAge=groupGender.substring(0, groupGender.indexOf('/'));//.toLowerCase();
       groupGender=groupGender.substring(groupGender.indexOf('/')+1, groupGender.length());
       globalData.currentGroup=groupGender.substring(0, groupGender.indexOf('/'));//.toLowerCase();
       globalData.currentGender=groupGender.substring(groupGender.indexOf('/')+1, groupGender.length());//.toLowerCase();
        
       Event.removeAllItems();
       
       // Load contents of event box
        HashSet validEvents = getEvents(globalData.currentAge,globalData.currentGroup, globalData.currentGender);
        
        int i;
        if (validEvents != null) 
        {
            //Event.addItem(globalData.eventInfo[0]);
            globalData.eventCount = 0;
            for (i = 0; i < Array.getLength(globalData.eventInfo); i++) 
            {
                if (validEvents.contains(globalData.eventInfo[i])) 
                {
                    Event.addItem(globalData.eventInfo[i]);
                    Event.setEnabled(true);
                    globalData.eventCount++;
                }
            }
        }
       
       
    }//GEN-LAST:event_GroupActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Viewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Viewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Viewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Viewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Viewer dialog = new Viewer(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
     //init the table
    public void init()
    {
      
        //clear everything
        for(int i=dataT.getRowCount()-1;i>=0;i--)
        {
            for(int j=dataT.getColumnCount()-1;j>=0;j--)
            {
                dataT.getModel().setValueAt("",i , j);
            }
        }    
    }
    
    //check is save is clicked
    public boolean SaveClicked()
    {
        return saveData;
    }
    //switches content of two rows
    public void switchRows(int row0, int row1)
    {
        for(int i=0;i<dataT.getColumnCount();i++)
        {
            String temp0=(String)dataT.getModel().getValueAt(row0 , i);
            String temp1=(String)dataT.getModel().getValueAt(row1 , i);
            dataT.getModel().setValueAt(temp1,row0 ,i);
            dataT.getModel().setValueAt(temp0,row1 ,i);
        }
    }
    
    private void setHeader(int rowCount)
    {
        DefaultTableModel tabModel=((DefaultTableModel) dataT.getModel());
        tabModel.setRowCount(0);
        tabModel.setColumnCount(24);
        tabModel.setRowCount(rowCount);
        
        String fields[]={
            "AthleteID",
            "FirstName",
            "LastName",
            "Email",
            "Gender",
            "College",
            "School",
            "Experience",
            "AgeGroup",
            "Event",
            "Nandu",
            "SetupInfo",
            "JudgeTypes",
            "JudgeConnected",
            "JudgeARaw",
            "JudgeAAgree",
            "JudgeBRaw",
            "JudgeCRaw",
            "JudgeCAgree",
            "Time",
            "Deductions",
            "Reason",
            "Score",
            "Original Order"
        };
        //change the name of each column
        for(int i=0;i<fields.length;i++)
            dataT.getColumnModel().getColumn(i).setHeaderValue(fields[i]);        
    }
    
    public void updateAthletes(Vector AthleteListFileData)
    {
        //AthleteListFileDataRaw=new Vector(AthleteListFileData);
        AthleteListFileDataOrdered=new Vector(AthleteListFileData);
        AthleteOriginalIndexOrdered.clear();
        
        setHeader(AthleteListFileData.size()+1);
        
        Vector eventNames=new Vector();
        //update teh cells
        for(int i=0;i<AthleteListFileData.size();i++)
        {
            Main.AthleteFileData ad=(Main.AthleteFileData)AthleteListFileData.elementAt(i);
            AthleteOriginalIndexOrdered.add(Integer.toString(i));
            String originalIndex=(String)AthleteOriginalIndexOrdered.elementAt(i);
            
            dataT.getModel().setValueAt((String)ad.AthleteID,i , 0);
            dataT.getModel().setValueAt(ad.FirstName,i , 1);
            dataT.getModel().setValueAt(ad.LastName,i , 2);
            dataT.getModel().setValueAt(ad.Email,i , 3);
            dataT.getModel().setValueAt(ad.Gender,i , 4);
            dataT.getModel().setValueAt(ad.College,i , 5);
            dataT.getModel().setValueAt(ad.School,i , 6);
            dataT.getModel().setValueAt(ad.Experience,i , 7);
            dataT.getModel().setValueAt(ad.AgeGroup,i , 8);
            dataT.getModel().setValueAt(ad.Event,i , 9);
            dataT.getModel().setValueAt(ad.Nandu,i , 10);
            dataT.getModel().setValueAt(ad.SetupInfo,i , 11);
            dataT.getModel().setValueAt(ad.JudgeTypes,i , 12);
            dataT.getModel().setValueAt(ad.JudgeConnected,i , 13);
            dataT.getModel().setValueAt(ad.JudgeARaw,i , 14);
            dataT.getModel().setValueAt(ad.JudgeAAgree,i , 15);
            dataT.getModel().setValueAt(ad.JudgeBRaw,i , 16);
            dataT.getModel().setValueAt(ad.JudgeCRaw,i , 17);
            dataT.getModel().setValueAt(ad.JudgeCAgree,i , 18);
            dataT.getModel().setValueAt(ad.Time,i , 19);
            dataT.getModel().setValueAt(ad.Deductions,i , 20);
            dataT.getModel().setValueAt(ad.Reason,i , 21);
            dataT.getModel().setValueAt(ad.Score,i , 22);
            dataT.getModel().setValueAt(originalIndex,i , 23);
            
            
            boolean found=false;
            for(int j=0;j<eventNames.size();j++)
            {
                if(((String)eventNames.get(j)).equals(ad.Event))
                {
                    found=true;
                    break;
                }
            }
            if(found==false)
                eventNames.add(ad.Event);
        }
        
        //add the unique events
        Event.removeAllItems();
        for(int j=0;j<eventNames.size();j++)
            Event.addItem(((String)eventNames.get(j)));
        
    }
    
    public void updateAthletesOrder(Vector AthleteListFileData)
    {
        //AthleteListFileDataRaw=new Vector(AthleteListFileData);
        AthleteListFileDataOrdered=new Vector(AthleteListFileData);
        AthleteOriginalIndexOrdered.clear();
        
        setHeader(AthleteListFileData.size()+1);
        
        /////////////////////////////////////////////////////////////////////////////////
        //add event to the combo box
        Vector eventNames=new Vector();
        for(int i=0;i<AthleteListFileData.size();i++)
        {
            Main.AthleteFileData ad=(Main.AthleteFileData)AthleteListFileData.elementAt(i);
            boolean found=false;
            for(int j=0;j<eventNames.size();j++)
            {
                if(((String)eventNames.get(j)).equals(ad.Event))
                {
                    found=true;
                    break;
                }
            }
            if(found==false)
                eventNames.add(ad.Event);
        }
        ////sort the names
        //Collections.sort(eventNames);
        
        for(int i=0;i<AthleteListFileData.size();i++)
        {
            AthleteOriginalIndexOrdered.add(Integer.toString(i));
        }
        /////////////////////////////////////////////////////////////////////////////////
        //update teh cells based on event order
        int addIndex=0;
        for(int j=0;j<eventNames.size();j++)
        {
            String currEvent=((String)eventNames.get(j));
            int index=0;
            for(int i=0;i<AthleteListFileData.size();i++)
            {
                Main.AthleteFileData ad=(Main.AthleteFileData)AthleteListFileData.elementAt(i);
                if(currEvent.toLowerCase().equals(ad.Event.toLowerCase())==false)
                    continue;
                AthleteOriginalIndexOrdered.setElementAt(Integer.toString(++index),i);
                String originalIndex=(String)AthleteOriginalIndexOrdered.elementAt(i);
                
                dataT.getModel().setValueAt((String)ad.AthleteID,addIndex , 0);
                dataT.getModel().setValueAt(ad.FirstName,addIndex , 1);
                dataT.getModel().setValueAt(ad.LastName,addIndex , 2);
                dataT.getModel().setValueAt(ad.Email,addIndex , 3);
                dataT.getModel().setValueAt(ad.Gender,addIndex , 4);
                dataT.getModel().setValueAt(ad.College,addIndex , 5);
                dataT.getModel().setValueAt(ad.School,addIndex , 6);
                dataT.getModel().setValueAt(ad.Experience,addIndex , 7);
                dataT.getModel().setValueAt(ad.AgeGroup,addIndex , 8);
                dataT.getModel().setValueAt(ad.Event,addIndex , 9);
                dataT.getModel().setValueAt(ad.Nandu,addIndex , 10);
                dataT.getModel().setValueAt(ad.SetupInfo,addIndex , 11);
                dataT.getModel().setValueAt(ad.JudgeTypes,addIndex , 12);
                dataT.getModel().setValueAt(ad.JudgeConnected,addIndex , 13);
                dataT.getModel().setValueAt(ad.JudgeARaw,addIndex , 14);
                dataT.getModel().setValueAt(ad.JudgeAAgree,addIndex , 15);
                dataT.getModel().setValueAt(ad.JudgeBRaw,addIndex , 16);
                dataT.getModel().setValueAt(ad.JudgeCRaw,addIndex , 17);
                dataT.getModel().setValueAt(ad.JudgeCAgree,addIndex , 18);
                dataT.getModel().setValueAt(ad.Time,addIndex , 19);
                dataT.getModel().setValueAt(ad.Deductions,addIndex , 20);
                dataT.getModel().setValueAt(ad.Reason,addIndex , 21);
                dataT.getModel().setValueAt(ad.Score,addIndex , 22);
                dataT.getModel().setValueAt(originalIndex,addIndex , 23);
                addIndex++;
            }
        }
        /////////////////////////////////////////////////////////////////////////////////
        
        /////////////////////////////////////////////////////////////////////////////////
        //add the unique events
        Event.removeAllItems();
        for(int j=0;j<eventNames.size();j++)
            Event.addItem(((String)eventNames.get(j)));
        /////////////////////////////////////////////////////////////////////////////////
        
    }
    
    private void updateAthleteList()
    {
       String groupGender = (String) Group.getSelectedItem();
       String currEvent=((String) Event.getSelectedItem());
       if(groupGender==null || groupGender.length()==0 || currEvent == null || currEvent.equals(""))
            return;
       globalData.currentEvent.setevent(currEvent);
       globalData.currentAge=groupGender.substring(0, groupGender.indexOf('/'));//.toLowerCase();
       groupGender=groupGender.substring(groupGender.indexOf('/')+1, groupGender.length());
       globalData.currentGroup=groupGender.substring(0, groupGender.indexOf('/'));//.toLowerCase();
       globalData.currentGender=groupGender.substring(groupGender.indexOf('/')+1, groupGender.length());//.toLowerCase();
       
       String currentEvent=globalData.currentEvent.getevent();
       String currentAge=globalData.currentAge;
       String currentGroup=globalData.currentGroup;
       String currentGender=globalData.currentGender;
       
       //set the header
       setHeader(AthleteListFileDataOrdered.size()+1);
       int addIndex=0;
       for(int i=0;i<AthleteListFileDataOrdered.size();i++)
       {
            Main.AthleteFileData ad=(Main.AthleteFileData)AthleteListFileDataOrdered.elementAt(i);
            if(ad.AgeGroup.equals(currentAge) && ad.Gender.equals(currentGender) && ad.Experience.equals(currentGroup) && ad.Event.equals(currentEvent))
            {
                String originalIndex=(String)AthleteOriginalIndexOrdered.elementAt(i);
                dataT.getModel().setValueAt((String)ad.AthleteID,addIndex , 0);
                dataT.getModel().setValueAt(ad.FirstName,addIndex , 1);
                dataT.getModel().setValueAt(ad.LastName,addIndex , 2);
                dataT.getModel().setValueAt(ad.Email,addIndex , 3);
                dataT.getModel().setValueAt(ad.Gender,addIndex , 4);
                dataT.getModel().setValueAt(ad.College,addIndex , 5);
                dataT.getModel().setValueAt(ad.School,addIndex , 6);
                dataT.getModel().setValueAt(ad.Experience,addIndex , 7);
                dataT.getModel().setValueAt(ad.AgeGroup,addIndex , 8);
                dataT.getModel().setValueAt(ad.Event,addIndex , 9);
                dataT.getModel().setValueAt(ad.Nandu,addIndex , 10);
                dataT.getModel().setValueAt(ad.SetupInfo,addIndex , 11);
                dataT.getModel().setValueAt(ad.JudgeTypes,addIndex , 12);
                dataT.getModel().setValueAt(ad.JudgeConnected,addIndex , 13);
                dataT.getModel().setValueAt(ad.JudgeARaw,addIndex , 14);
                dataT.getModel().setValueAt(ad.JudgeAAgree,addIndex , 15);
                dataT.getModel().setValueAt(ad.JudgeBRaw,addIndex , 16);
                dataT.getModel().setValueAt(ad.JudgeCRaw,addIndex , 17);
                dataT.getModel().setValueAt(ad.JudgeCAgree,addIndex , 18);
                dataT.getModel().setValueAt(ad.Time,addIndex , 19);
                dataT.getModel().setValueAt(ad.Deductions,addIndex , 20);
                dataT.getModel().setValueAt(ad.Reason,addIndex , 21);
                dataT.getModel().setValueAt(ad.Score,addIndex , 22);
                dataT.getModel().setValueAt(originalIndex,addIndex , 23);
                addIndex++;
            }    
       }
       DefaultTableModel tabModel=((DefaultTableModel) dataT.getModel());
       tabModel.setRowCount(addIndex+1);
    }
    
    public void enableModifyAthletes()
    {
        jLabel1.setEnabled(false);
        jLabel2.setEnabled(false);
        Group.setEnabled(false);
        Event.setEnabled(false); 
        RandOrderB.setEnabled(false); 
        addAthletesClicked=true;
    }
    
    public void enableRandomizeAthletes()
    {
        jLabel1.setEnabled(true);
        jLabel2.setEnabled(true);
        Group.setEnabled(true);
        Event.setEnabled(true);   
        RandOrderB.setEnabled(true); 
        addAthletesClicked=false;
        
    }
    //updates group
    public void updateGroups(HashSet groupList)
    {
        Group.removeAllItems();
        for (int i = 0; i < Array.getLength(globalData.groupInfo); i++)
        {
            if(groupList.contains(globalData.groupInfo[i]))
                Group.addItem(globalData.groupInfo[i]);
        }   
    }
    
    //verifys nandu
    private boolean verifyNandu(String data,String event)
    {

        String tempNandu=new String(data) ;
        String value="";
        int count=0;
        while(tempNandu.length()!=0)
        {
            
            if(tempNandu.indexOf(",")==-1)
            {
                value=tempNandu;
                tempNandu="";
            }
            else
            {
               value=tempNandu.substring(0, tempNandu.indexOf(","));
               tempNandu=tempNandu.substring(tempNandu.indexOf(",")+1);
            }
            
            if(nandu.find(value,event)==null)
            {
                JOptionPane.showMessageDialog(this, "Cannot find nandu "+value+" in the event "+event+" \n" , "Error", JOptionPane.DEFAULT_OPTION);
                return false;
            }
            count++;
        }
        return true;
    }
    
    //set Nandu Data.
    public void setNanduData(nanduData nData)
    {
        nandu=nData;        
    }
    
    //returns the athletes list
    public Vector getAthletesList()
    {
        return AthleteListFileDataOrdered;
    }
    
        //returns events
    public HashSet getEvents(String ageGroup, String level, String gender)
    {
        HashSet eventSet = new HashSet();
        
        for(int i=0;i<AthleteListFileDataOrdered.size();i++)
        {
            Main.AthleteFileData ad=(Main.AthleteFileData)AthleteListFileDataOrdered.elementAt(i);
            if(ad.AgeGroup.equals(ageGroup) && ad.Gender.equals(gender) && ad.Experience.equals(level))
            {
                eventSet.add(ad.Event);
            }
        }
        return eventSet;
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelB;
    private javax.swing.JComboBox Event;
    private javax.swing.JComboBox Group;
    private javax.swing.JButton RandOrderB;
    private javax.swing.JButton SaveB;
    private javax.swing.JTable dataT;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
